// pages/productdetails/productdetails.js
var app = getApp();
var GlobalData = require('../../utils/GlobalData.js');
var dataManager = require('../../utils/dataManager.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    detailsData: {},
    payType: 0,
    selArr: [true, false, false],
    payTypeArr: [],
    num: 1,
    productDetailprice: { bonuspoints: 0, cash: 0 }

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      detailsData: GlobalData.productDetails,
    });
    // this.detailsData = GlobalData.productDetails;
    // this.productDetailprice = this.detailsData.exchanges[0]
    for (let j = 0; j < this.data.detailsData.imgs.length; j++) {
      if (this.data.detailsData.imgs[j].sortno == 1) {
        this.data.detailsData['img'] = this.data.detailsData.imgs[j].path;
      }
    }
    for (let i = 0; i < this.data.detailsData.exchanges.length; i++) {
      if (this.data.detailsData.exchanges[i].cash == 0 && this.data.detailsData.exchanges[i].bonuspoints == 0)
        continue;
      else
        this.data.payTypeArr.push(this.data.detailsData.exchanges[i])

    }

    this.setData({
      detailsData: this.data.detailsData,
      productDetailprice: this.data.detailsData.exchanges[0],
      payTypeArr: this.data.payTypeArr,
    })
    // this.payTypeArr = GlobalData.clone(this.detailsData.exchanges);

  },

  onPayType(type) {
    this.setData({
      payType: type.currentTarget.dataset.id
    })
    for (let i = 0; i < 3; i++) {
      if (i == this.data.payType)
        this.data.selArr[i] = true;
      else
        this.data.selArr[i] = false;
    }
    this.setData({
      selArr: this.data.selArr,
      productDetailprice: this.data.payTypeArr[this.data.payType]
    })
  },
  /**加入购物小车 */
  onJoincar() {
    if (this.data.num > this.data.detailsData.stock) {
      app.onshowModal("库存不足");
      return;
    }
    if (!this.data.num) {
      app.onshowModal("请添加商品数量");
      return;
    }
    let obj = {};
    obj["id"] = 0;
    obj["goodsid"] = this.data.detailsData.id;
    obj["bonuspoints"] = this.data.payTypeArr[this.data.payType].bonuspoints;
    obj["cash"] = this.data.payTypeArr[this.data.payType].cash;
    obj["num"] = this.data.num;
    dataManager.rigisterObserver("updateshoppingcart", this.onCompleteAddCar, this);
    dataManager.onAddCar(obj);
  },
  onCompleteAddCar() {
    app.onshowModal("添加购物车成功");
    dataManager.getShoppingCar();
  },
  /**加减 */
  onChangeInfo(num) {
    let count = parseInt(num.currentTarget.dataset.id)
    if (count == 1) {
      this.data.num = this.data.num - 1 > 0 ? this.data.num - 1 : 0
    } else {
      this.data.num += 1;
    }
    this.setData({
      num: this.data.num,
    })
  },
  /**立即兑换 */
  onExchange() {
    if (this.data.num == 0) {
      app.onshowModal("请添加商品数量");
      return;
    }
    let pri = this.data.payTypeArr[this.data.payType].cash * this.data.num;
    let fen = this.data.payTypeArr[this.data.payType].bonuspoints * this.data.num;

    if (this.data.num > this.data.detailsData.stock) {
      app.onshowModal("库存不足");
      return;
    }
    if (fen > GlobalData.homeData.bonuspoints) {
      app.onshowModal("积分不足，请更换兑换方式。");
      return;
    }
    GlobalData.buyInfo = [];
    let obj = {};
    obj = this.data.detailsData;
    obj["num"] = this.data.num;
    obj["bonuspoints"] = this.data.payTypeArr[this.data.payType].bonuspoints
    obj["cash"] = this.data.payTypeArr[this.data.payType].cash
    obj["goodsid"] = this.data.detailsData.id;
    // obj["goodsname"] = this.data.goodsname;
    // obj["description"] = this.data.description;
    obj["imgpath"] = this.data.detailsData.imgs;

    GlobalData.buyInfo.push(obj);
    let objPrice = {};
    objPrice["bonuspoints"] = this.data.payTypeArr[this.data.payType].bonuspoints * parseInt(this.data.num)
    objPrice["cash"] = this.data.payTypeArr[this.data.payType].cash * parseInt(this.data.num)
    GlobalData.price = GlobalData.clone(objPrice);
    GlobalData.isdirectbuy = true;
    dataManager.UseNavigateTo("/pages/integralconfirm/integralconfirm", false);

    // this.router.navigateByUrl("productconfirm/productdetails" + GlobalData._random())
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // }
})